package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tools.ant.taskdefs.condition.IsSet;

import models.NewsModel;

import admin.data.NewsData;


public class ProcessNewsServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String strIds = (String) request.getParameter("ids");
	    String action = (String) request.getParameter("action");
	    String[] ids = strIds.split(",");
	    NewsData newsData = new NewsData();
	    
	    ArrayList<String> statusAction = new ArrayList<String>(Arrays.asList("status_publish", "status_unpublish", "status_hotnews", "status_unhotnews"));
	    
	    for (int i = 0; i < ids.length; i++) {
	        if (!ids[i].isEmpty()) {
	        	if(statusAction.contains(action.toLowerCase())) {
	        		NewsModel news = newsData.getNews(URLDecoder.decode(ids[i]));
	                if (news != null) {
	                	if (action.equalsIgnoreCase("status_publish")) {
		                	news.setPublished(true);
		                } else if (action.equalsIgnoreCase("status_unpublish")) {
		                	news.setPublished(false);
						} else if (action.equalsIgnoreCase("status_hotnews")) {
							news.setHotNews(true);
						} else if (action.equalsIgnoreCase("status_unhotnews")) {
							news.setHotNews(false);
						}
	                	newsData.insert(news);
	        		}
	        	} else if (action.equalsIgnoreCase("delete")) {
	        		NewsModel news = newsData.getNews(URLDecoder.decode(ids[i]));
	        		newsData.delete(news);
				}
	        }
	    }
	}
}
